package com.ruoyi.web.controller.chemistry;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.DangerChemicalQueryDetailedList;
import com.ruoyi.system.service.IDangerChemicalQueryDetailedListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (DangerChemicalQueryDetailedList)表控制层
 *
 * @author makejava
 * @since 2022-11-14 17:38:15
 */
@RestController
@RequestMapping("danger/chemical/query/detailed")
public class DangerChemicalQueryDetailedListController {
    /**
     * 服务对象
     */
    @Autowired
    private IDangerChemicalQueryDetailedListService dangerChemicalQueryDetailedListService;

    @GetMapping("list")
    public TableDataInfo list(DangerChemicalQueryDetailedList request) {
        startPage();
        List<DangerChemicalQueryDetailedList> list = dangerChemicalQueryDetailedListService.list(request);
        return getDataTable(list);
    }

//    public ResponseEntity<Page<DangerChemicalQueryDetailedList>> queryByPage(DangerChemicalQueryDetailedList dangerChemicalQueryDetailedList, PageRequest pageRequest) {
//        return ResponseEntity.ok(this.dangerChemicalQueryDetailedListService.queryByPage(dangerChemicalQueryDetailedList, pageRequest));
//    }

    @GetMapping("count")
    public AjaxResult count() {
        List<DangerChemicalQueryDetailedList> list1 = dangerChemicalQueryDetailedListService.list(new DangerChemicalQueryDetailedList());
        DangerChemicalQueryDetailedList dangerChemicalQueryDetailedList = new DangerChemicalQueryDetailedList();
        dangerChemicalQueryDetailedList.setIzKeySupervision("是");
        List<DangerChemicalQueryDetailedList> list2 = dangerChemicalQueryDetailedListService.list(dangerChemicalQueryDetailedList);
        Map<String,Integer> map = new HashMap<>();
        map.put("total", CollUtil.isEmpty(list1) ? 0 : list1.size());
        map.put("jianguan", CollUtil.isEmpty(list2) ? 0 : list2.size());
        return AjaxResult.success(map);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<DangerChemicalQueryDetailedList> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.dangerChemicalQueryDetailedListService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param dangerChemicalQueryDetailedList 实体
     * @return 新增结果
     */
    @PostMapping("add")
    @PreAuthorize("@ss.hasPermi('danger:chemical:query:detailed:add')")
    public ResponseEntity<DangerChemicalQueryDetailedList> add(@Validated @RequestBody DangerChemicalQueryDetailedList dangerChemicalQueryDetailedList) {
        return ResponseEntity.ok(this.dangerChemicalQueryDetailedListService.insert(dangerChemicalQueryDetailedList));
    }

    /**
     * 编辑数据
     *
     * @param dangerChemicalQueryDetailedList 实体
     * @return 编辑结果
     */
    @PutMapping("update")
    @PreAuthorize("@ss.hasPermi('danger:chemical:query:detailed:update')")
    public ResponseEntity<DangerChemicalQueryDetailedList> edit(@Validated @RequestBody DangerChemicalQueryDetailedList dangerChemicalQueryDetailedList) {
        return ResponseEntity.ok(this.dangerChemicalQueryDetailedListService.update(dangerChemicalQueryDetailedList));
    }

    /**
     * 删除数据
     *
     * @param dangerChemicalQueryDetailedList 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    @PreAuthorize("@ss.hasPermi('danger:chemical:query:detailed:delete')")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody DangerChemicalQueryDetailedList dangerChemicalQueryDetailedList) {
        Integer id = dangerChemicalQueryDetailedList.getId();
        return ResponseEntity.ok(this.dangerChemicalQueryDetailedListService.deleteById(id));
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageUtils.startPage();
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

}

